<?php

/**
 * Created by PhpStorm.
 * User: wyr
 * Date: 14-10-8
 * Time: 下午10:14
 */
class Controller_Wwwmall2_Index extends Controller_Wwwmall2_Abstract
{

    function showMessage($message, $msgType, $url = null)
    {
        echo "<script>alert('{$message}');";
        if ($url) echo "location='{$url}';";
        echo "</script>";
    }


    /*
     * 商城首页
     */
    public function defaultAction()
    {
        global $_F;
//        $this->openDebug();

        $chainTable = new FTable("chain_shop", "", "xhs_slave");
        $shop = $chainTable->where("id=4")->find();
        $time = time();
        if($time < strtotime(date('Y-m-d') . $shop['start_work_time']) || $time > strtotime(date('Y-m-d') . $shop['stop_work_time'])){
            $store_close_words = $this->config['store_close_words'];
            $store_close_words = str_replace('{$start_time}',substr($shop['start_work_time'],0,5),$store_close_words);
            $store_close_words = str_replace('{$end_time}',substr($shop['stop_work_time'],0,5),$store_close_words);
            $this->assign('store_close_words',$store_close_words);
        }

        //获取banner列表
        $banner_list = Service_Goods::getBannerList();
        foreach ($banner_list as $k => $v) {
            $banner_list[$k]['url'] = Service_Attach::getInstance()->getThumbUrl($v['url']);
        }
        $this->assign("banner_list", $banner_list);

        //获取商品分类
        $cate = Service_Goods::getCategoriesGoodsList();
        foreach ($cate as $k => $v) {
            $cate[$k]['icon'] = Service_Attach::getInstance()->getThumbUrl($v['icon'], 80, 80);
        }
        $this->assign("cate", $cate);

        //获取商品列表
        $good_list = Service_Goods::getGoodsList(2, 1, 100);
        foreach ($good_list as $k => $v) {
            $good_list[$k]['img3'] = Service_Attach::getInstance()->getThumbUrl($v['img2'], 640, 280);
            $good_list[$k]['img2'] = Service_Attach::getInstance()->getThumbUrl($v['img2'], 250, 250);
        }
        $this->assign("good_list", $good_list);

        //获取购物车列表
        $cart_list = Service_Goods::getCartGoods();
        $this->assign("cart_list", $cart_list);
        //获取购物车数量
        $cart_list_count = $cart_list_price = 0;
        foreach ($cart_list as $k => $v) {
            $cart_list_count += $cart_list[$k]['cart_num'];
            $cart_list_price += $cart_list[$k]['price_new'] * $cart_list[$k]['cart_num'];
        }
        $this->assign("cart_list_count", $cart_list_count);
        $this->assign("cart_list_price", $cart_list_price);

        //还剩多少钱起送
        if ($cart_list_price < $this->send_low_money) {
            $this->assign("cart_list_price_sx", $this->send_low_money - $cart_list_price);
        }

        $pro_cart_tips = $this->cart_tips($cart_list_price);
        $this->assign("pro_cart_tips", $pro_cart_tips);


        Service_VisitLog::add($_F['uid'],1);

        $this->display('index');
    }

    /**
     * 首页定位
     */
    public function getLocationAction()
    {
        FCookie::remove('_base');
        FResponse::redirect('/location/index');
    }

    public function goodDetailAction()
    {
//        $this->openDebug();
        $id = FRequest::getInt("id") ? FRequest::getInt("id") : 0;

        if ($id > 0) {
            //获取购物车列表
            $cart_list = Service_Goods::getCartGoods();
            $this->assign("cart_list", $cart_list);
            //获取购物车数量   获取购物车总价
            $cart_list_count = $cart_list_price = 0;
            foreach ($cart_list as $k => $v) {
                $cart_list_count += $cart_list[$k]['cart_num'];
                $cart_list_price += $cart_list[$k]['price_new'] * $cart_list[$k]['cart_num'];
            }
            $this->assign("cart_list_count", $cart_list_count);
            $this->assign("cart_list_count", count($cart_list));
            $this->assign("cart_list_price", $cart_list_price);

            //还剩多少钱起送
            if ($cart_list_price < $this->send_low_money) {
                $this->assign("cart_list_price_sx", $this->send_low_money - $cart_list_price);
            }

            $pro_cart_tips = $this->cart_tips($cart_list_price);
            $this->assign("pro_cart_tips", $pro_cart_tips);

            //获取商品详细信息
            $info = Service_Goods::getGoods($id);

            //$info['img2'] = Service_Attach::getInstance()->getThumbUrl($info['img2']);

            if(empty($info['img1'])){
                $info['img1'] = $info['img2'];
            }else
                $info['img1'] = $info['img2'] . ',' . $info['img1'];

            $info['img1'] = explode(',',$info['img1']);
            if(count($info['img1']) > 0)
                foreach ($info['img1'] as &$img1){
                    $img1 = Service_Attach::getInstance()->getThumbUrl($img1);
                }
            //$info['img1'] = array_reverse($info['img1']);
            //array_unshift($info['img1'],$info['img2']);
            /*$img2Array = explode(',', $info['img2']);
            foreach ($img2Array as $v) {
                $img2_Array[] = array(
                    'pic' => Service_Attach::getInstance()->getThumbUrl(str_replace('_192_192.jpg', '', $v)),
                );
            }
            $info['img2'] = $img2_Array;*/
            $this->assign("info", $info);

            //获取推荐列表
            $recommendList = Service_Goods::getRecommendGoodsList();
            $this->assign("recommendList", $recommendList);

            $this->display('good/good-detail');
        } else {
            $this->error('参数有误', '/');
        }

    }

    /**
     * 清空购物车
     */
    public function clearCartAction()
    {
        Service_Goods::clearCart();
    }

    /**
     * 购物车操作
     */
    public function modifyCartAction()
    {
        $id = FRequest::getInt("id") ? FRequest::getInt("id") : 0;
        $num = FRequest::getInt("num") ? FRequest::getInt("num") : 0;

        if ($id > 0 && $num > 0) {
            if ($num == 99) $num = -1;
            Service_Goods::updateCart($id, $num);
        }

    }

    /**
     * 重新加载购物车
     */
    public function loadCartAction()
    {
        //获取购物车列表
        $cart_list = Service_Goods::getCartGoods();
        //获取购物车数量
        $cart_list_count = $cart_list_price = 0;
        foreach ($cart_list as $k => $v) {
            $cart_list_count += $cart_list[$k]['cart_num'];
            $cart_list_price += $cart_list[$k]['price_new'] * $cart_list[$k]['cart_num'];
        }
        $tips = $this->cart_tips($cart_list_price);
        $result = array('status' => 200, "data" => $cart_list,"tips"=>$tips);
        FResponse::output($result);
    }

    public function inAction()
    {
//        $this->openDebug();
        $this->display('add_address');
    }

    public function testAction()
    {
        FCache::delete("province_list_20170601");
        $data = Service_User::getProvince();
        var_dump($data);
        exit();
    }

    /*
     * 获取城市信息
     */
    public function getCityAction()
    {
        $provinceCode = FRequest::getPostString("pro_code") ? FRequest::getPostString("pro_code") : "";
        if ($provinceCode) {
            $list = Service_User::getCity($provinceCode);
            if ($list) {
                $result = array('status' => 200, "data" => $list);
            } else {
                $result = array("status" => -1, "error" => "城市数据未查询到");
            }
        } else {
            $result = array("status" => -2, "error" => "省份id为空");
        }
        FResponse::output($result);
    }


    public function userOrderDealAction()
    {
        var_dump($_POST);
        exit();
    }


}